package com.栈;

import java.util.Scanner;
import java.util.Stack;

/**
 * 用两个栈就可以实现队列操作
 */
public class 用栈实现队列 {
    static class MyQueue {
        Stack<Integer> stackA ;
        Stack<Integer> stackB;
        /** Initialize your data structure here. */
        public MyQueue() {
            stackA= new Stack<>();
            stackB = new Stack<>();
        }

        /** Push element x to the back of queue. */
        public void push(int x) {
            stackA.push(x);
        }

        /**. */
        public int pop() {
            //只有B栈为空才能压入
            if(stackB.isEmpty()){
                this.pushB();
            }
            //压入后再次判断B栈是否为空 ，是空的话就说明两个栈都是空的
            if(stackB.isEmpty()){
                return -1;
            }
            else {
                return stackB.pop();
            }
        }

        /**
         * 将A栈循环压入B栈
         */
        public void pushB(){
            while (!stackA.empty()){
                stackB.push(stackA.pop());
            }

        }
        /** Get the front element. */
        /**
         * 和弹出的逻辑一样
         * @return
         */
        public int peek() {
            if(stackB.isEmpty()){
                this.pushB();
            }
            if(stackB.isEmpty()){
                return -1;
            }
            else {
                return stackB.peek();
            }
        }

        /**  */
        public boolean empty() {
            //先判断B栈 再判断A栈
           if(stackB.isEmpty()){
               if(stackA.isEmpty()){
                   return true;
               }
               else{
                   return false;
               }
           }
           else{
            return false;
           }

        }

        @Override
        public String toString() {
            return "MyQueue{" +
                    "stackA=" + stackA +
                    ", stackB=" + stackB +
                    '}';
        }
    }

    public static void main(String[] args) {
        MyQueue stack = new MyQueue();
        int  i;
        while (true){
            // System.out.println("出队输入0");
            //System.out.println("结束输入-1");
            //-2  查询栈头
            //-3 最小栈
            System.out.println("输入入栈数字：");
            i = new Scanner(System.in).nextInt();
            if(i == -2){

                System.out.println("栈头:"+stack.peek());
                System.out.println(stack.toString());
                continue;

            }



            if(i == -1){
                System.out.println("结束栈");
                break;
            }
            if(i==0){

                stack.pop();
                System.out.println(stack.toString());
                continue;
            }
            if(i==-4){
                System.out.println(stack.empty());
            }
            //入栈
            stack.push(i);
            System.out.println(stack.toString());



        }
    }
}
